﻿namespace Raven
{
    public class GoalGetItem : GoalComposite<RavenBot>
    {
        public static GoalType ItemTypeToGoalType(EntityType gt)
        {
            switch (gt)
            {
                case EntityType.Health:

                    return GoalType.goal_get_health;
                case EntityType.RailGunWeaponGiver:

                    return GoalType.goal_get_railgun;
                case EntityType.RocketLauncherWeaponGiver:

                    return GoalType.goal_get_rocket_launcher;
                case EntityType.ShotgunWeaponGiver:

                    return GoalType.goal_get_shotgun;

                default:
                    Logger.LogError("Goal_GetItem cannot determine item type");
                    return GoalType.goal_none;
            } //end switch
        }

        private EntityType _itemToGet;

        private Trigger<RavenBot> _giverTrigger;

        //true if a path to the item has been formulated
        private bool _followingPath;

        //returns true if the bot sees that the item it is heading for has been
        //picked up by an opponent
        bool hasItemBeenStolen()
        {
            if (_giverTrigger != null &&
                !_giverTrigger.isActive() &&
                _owner.hasLOSto(_giverTrigger.Pos()))
            {
                return true;
            }

            return false;
        }


        public GoalGetItem(RavenBot owner, EntityType entityType) : base(owner, ItemTypeToGoalType(entityType))
        {
            _itemToGet = entityType;
            _giverTrigger = null;
            _followingPath = false;
        }

        public override void Activate()
        {
            _status = GoalStatus.active;

            _giverTrigger = null;

            //request a path to the item
            _owner.GetPathPlanner().RequestPathToItem(_itemToGet);

            //the bot may have to wait a few update cycles before a path is calculated
            //so for appearances sake it just wanders
            AddSubgoal(new GoalWander(_owner));
        }

        public override GoalStatus Process()
        {
            ActivateIfInactive();

            if (hasItemBeenStolen())
            {
                Terminate();
            }

            else
            {
                //process the subgoals
                _status = ProcessSubgoals();
            }

            return _status;
        }

        public override void Terminate()
        {
            _status = GoalStatus.completed;
        }
        
        public override bool HandleMessage(Telegram msg)
        {
            //first, pass the message down the goal hierarchy
            bool bHandled = ForwardMessageToFrontMostSubgoal(msg);

            //if the msg was not handled, test to see if this goal can handle it
            if (bHandled == false)
            {
                switch(msg.msg)
                {
                    case MessageType.Msg_PathReady:

                        //clear any existing goals
                        RemoveAllSubgoals();

                        AddSubgoal(new GoalFollowPath(_owner,
                            _owner.GetPathPlanner().GetPath()));

                        //get the pointer to the item
                        _giverTrigger = msg.extraInfo as Trigger<RavenBot>;

                        return true; //msg handled


                    case MessageType.Msg_NoPathAvailable:

                        _status = GoalStatus.failed;

                        return true; //msg handled

                    default: return false;
                }
            }

            //handled by subgoals
            return true;
        }
    }
}